#include "StdAfx.h"
#include "DealingRepository.h"
#include "sqlite3/sqlite3x.hpp"

const char* CREATE_SQL = 
	"create table t_dealing( \
	request_id int primary key, \
	business_id, \
	contract char(20), \
	contract_index int, \
	direction char(1), \
	operation char(1), \
	order_quantity int, \
	order_price double, \
	system_no char(50), \
	status char(1), \
	avg_price double, \
	deal_quantity int, \
	remain_quantity int, \
	cancel_quantity int, \
	order_time char(30), \
	last_modify_time char(30) \
	)";
const char* INSERT_SQL = 
	"insert into t_dealing \
	(request_id,business_id, contract,contract_index,direction,operation,\
	order_quantity,order_price,system_no,status,avg_price,deal_quantity,\
	remain_quantity,cancel_quantity,order_time,last_modify_time) \
	values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

const char* UPDATE_SQL = 
"update t_dealing set business_id = ?,system_no = ?,status = ?,avg_price = ?,deal_quantity = ?,\
	remain_quantity = ?,cancel_quantity = ?,last_modify_time = ? \
	where request_id = ?";

const char* SELECT_SQL = 
	"select request_id,business_id,contract,contract_index,direction,operation,\
	order_quantity,system_no,status,avg_price,deal_quantity,\
	remain_quantity,cancel_quantity,order_time,last_modify_time \
	from t_dealing where request_id = ?";


DealingRepository::DealingRepository()
{
}

// DealingRepository::DealingRepository(sqlite3x::connection& dbConnection)
// :m_dbConnection(dbConnection)
// {
// 	Create();
// }

DealingRepository::~DealingRepository(void)
{
}


Dealing& DealingRepository::Save(Dealing const& dealing)
{
	std::map<int,Dealing>::iterator iter = m_dealings.find(dealing.GetLocalId());
	if (iter == m_dealings.end())
	{
		iter = m_dealings.insert(std::make_pair(dealing.GetLocalId(),dealing)).first;
	}
	Dealing& savedDealing = iter->second;
	savedDealing = dealing;
	// 	sqlite3x::transaction trans(m_dbConnection);
	// 	if (IsExist(dealing.GetRequestId()))
	// 	{
	// 		Update(dealing);
	// 	}
	// 	else
	// 	{
	// 		Insert(dealing);
	// 	}
	// 	trans.commit();

	return savedDealing;
}

Dealing* DealingRepository::Get(int localId)
{
	std::map<int,Dealing>::iterator iter = m_dealings.find(localId);
	if (iter == m_dealings.end())
	{
		return NULL;
	}
	return &iter->second;
}

char const* time_now(char buff[])
{
	SYSTEMTIME st = {0};
	::GetLocalTime(&st);
	sprintf_s(buff,30,"%04d%02d%02d %02d:%02d:%02d.%03d",st.wYear,st.wMonth,st.wDay, st.wHour,st.wMinute,st.wSecond,st.wMilliseconds);
	return buff;
}
 
// void DealingRepository::Insert(Dealing const& dealing)
// {
// 	sqlite3x::command cmd(m_dbConnection,INSERT_SQL);
// 	char now[40] = "";
// 	time_now(now);
// 	int cnt = 1;
// 	cmd.bind(cnt++, dealing.GetRequestId());
// 	cmd.bind(cnt++,dealing.GetBusinessId());
// 	cmd.bind(cnt++,dealing.GetContractCode());
// 	cmd.bind(cnt++,dealing.GetContract());
// 	cmd.bind(cnt++,dealing.GetDirectionFlag());
// 	cmd.bind(cnt++,dealing.GetOperationFlag());
// 	cmd.bind(cnt++,dealing.GetOrderQuantity());
// 	cmd.bind(cnt++,dealing.GetOrderPrice());
// 	cmd.bind(cnt++,dealing.GetSystemNo());
// 	cmd.bind(cnt++,dealing.GetStatus());
// 	cmd.bind(cnt++,dealing.GetAverageDealPrice());
// 	cmd.bind(cnt++,dealing.GetDealQuantity());
// 	cmd.bind(cnt++,dealing.GetRemainQuantity());
// 	cmd.bind(cnt++,dealing.GetCancelQuantity());
// 	cmd.bind(cnt++,now);
// 	cmd.bind(cnt++,now);
// 
// 	cmd.executenonquery();
// 
// }
// 
// void DealingRepository::Update(Dealing const& dealing)
// {
// 	sqlite3x::command cmd(m_dbConnection,UPDATE_SQL);
// 	char now[40] = "";
// 	time_now(now);
// 	int cnt = 1;
// 	cmd.bind(cnt++,dealing.GetBusinessId());
// 	cmd.bind(cnt++,dealing.GetSystemNo());
// 	cmd.bind(cnt++,dealing.GetStatus());
// 	cmd.bind(cnt++,dealing.GetAverageDealPrice());
// 	cmd.bind(cnt++,dealing.GetDealQuantity());
// 	cmd.bind(cnt++,dealing.GetRemainQuantity());
// 	cmd.bind(cnt++,dealing.GetCancelQuantity());
// 	cmd.bind(cnt++,now);
// 	cmd.bind(cnt++,dealing.GetRequestId());
// 
// 	cmd.executenonquery();
// }
// 
// bool DealingRepository::IsExist(int requestID) const
// {
// 	sqlite3x::command cmd(m_dbConnection,"select count(requestID) from t_dealing where request_id = ?");
// 	cmd.bind(1,requestID);
// 
// 	int count = cmd.executeint();
// 	
// 	return count > 0;
// }
// 
// void DealingRepository::Create()
// {
// 	sqlite3x::command cmd(m_dbConnection,"select count(name) from sqlite_master where type='table' and name=?");
// 	cmd.bind(1,"t_dealing");
// 
// 	if (cmd.executeint() > 0) return;
// 
// 	sqlite3x::command createCmd(m_dbConnection,CREATE_SQL);
// 	cmd.executenonquery();
// }

DealingRepository& DealingRepository::Instance()
{
	static DealingRepository s_repository;
	return s_repository;
}